# LuckPay Web Manage System
#
# Copyright (c) 2016 Lucky Byte, Inc.
#
express = require 'express'
pgsql   = require 'pg'
router = express.Router()
module.exports = router

router.get '/', (req, res, next) ->
    res.locals.breadcrumbs.push 'title': '站内通知'

    await pgsql.connect settings.pgsql_url, defer err, client, done
    if err then done(client); return next(err)

    # 用户列表
    await client.query \
        "select * from web_users where uuid <> $1 and disabled = false", [
            req.session.userinfo.uuid
        ], defer err, result
    if err then done(client); return next(err)
    res.locals.users = result.rows

    # 未读留言
    await client.query \
        "select m.*, u.realname as username from web_messages as m
            left join web_users as u on m.sender = u.uuid
            where m.status = 1 and m.recver = $1", [
                req.session.userinfo.uuid
            ], defer err, result
    if err then done(client); return next(err)
    res.locals.notify.messages = result.rows

    # 已发对方未读留言
    await client.query \
        "select m.*, u.realname as username from web_messages as m
            left join web_users as u on m.recver = u.uuid
            where m.status = 1 and m.sender = $1", [
                req.session.userinfo.uuid
            ], defer err, result
    if err then done(client); return next(err)
    res.locals.notify.messages_send = result.rows

    # 未读系统通知
    await client.query \
        "select * from web_notify where status = 1
            order by serial desc", defer err, result
    if err then done(client); return next(err)
    res.locals.notify.notifications = result.rows

    # 待复核商户记录
    if req.session.notify.merchs_wait_check.length > 0
        uuids = ''
        for merch in req.session.notify.merchs_wait_check
            uuids += "'#{merch.uuid}',"
        uuids = uuids.substring 0, uuids.length - 1
        await client.query "select m.*, u.realname as input_user
            from pay_merch_ex as m
            left join web_users as u on u.uuid = m.inputer
            where m.uuid in ( #{uuids} )", defer err, result
        if err then done(client); return next(err)
        res.locals.notify.merchs_wait_check = result.rows

    # 当前用户录入后被复核拒绝的商户记录
    if req.session.notify.merchs_rejected.length > 0
        uuids = ''
        for merch in req.session.notify.merchs_rejected
            uuids += "'#{merch.uuid}',"
        uuids = uuids.substring 0, uuids.length - 1
        await client.query "select m.*, u.realname as check_user
            from pay_merch_ex as m
            left join web_users as u on u.uuid = m.checker
            where m.uuid in ( #{uuids} )", defer err, result
        if err then done(client); return next(err)
        res.locals.notify.merchs_rejected = result.rows

    # 当前用户录入后复核通过的商户记录
    if req.session.notify.merchs_checked.length > 0
        uuids = ''
        for merch in req.session.notify.merchs_checked
            uuids += "'#{merch.uuid}',"
        uuids = uuids.substring 0, uuids.length - 1
        await client.query "select m.*, u.realname as check_user
            from pay_merch_ex as m
            left join web_users as u on u.uuid = m.checker
            where m.uuid in ( #{uuids} )", defer err, result
        if err then done(client); return next(err)
        res.locals.notify.merchs_checked = result.rows

    # 当前用户录入后未复核的商户记录
    if req.session.notify.merchs_input.length > 0
        uuids = ''
        for merch in req.session.notify.merchs_input
            uuids += "'#{merch.uuid}',"
        uuids = uuids.substring 0, uuids.length - 1
        await client.query "select m.* from pay_merch_ex as m
            where m.uuid in ( #{uuids} )", defer err, result
        if err then done(client); return next(err)
        res.locals.notify.merchs_input = result.rows

    # 当前用户复核后未生效的商户记录
    if req.session.notify.merchs_wait_effect.length > 0
        uuids = ''
        for merch in req.session.notify.merchs_wait_effect
            uuids += "'#{merch.uuid}',"
        uuids = uuids.substring 0, uuids.length - 1
        await client.query "select m.* from pay_merch_ex as m
            where m.uuid in ( #{uuids} )", defer err, result
        if err then done(client); return next(err)
        res.locals.notify.merchs_wait_effect = result.rows

    # 待复核终端记录
    terms_wait_check = []
    for term in req.session.notify.terms_wait_check
        await client.query \
            "select t.*, u.realname as input_user,
                m.name as mer_name, m.merno as mer_no
                from pay_terms_ex as t
                left join web_users as u on t.inputer = u.uuid
                left join pay_merch as m on m.uuid = t.merid
                where t.uuid = $1", [
                term.uuid
            ], defer err, result
        if err then done(client); return next(err)
        terms_wait_check.push result.rows...
    res.locals.notify.terms_wait_check = terms_wait_check

    # 当前用户录入后被复核拒绝的终端记录
    terms_rejected = []
    for term in req.session.notify.terms_rejected
        await client.query \
            "select t.*, m.name as mer_name, m.merno as mer_no
                from pay_terms_ex as t
                left join pay_merch as m on m.uuid = t.merid
                where uuid = $1", [
                term.uuid
            ], defer err, result
        if err then done(client); return next(err)
        terms_rejected.push result.rows...
    res.locals.notify.terms_wait_check = terms_wait_check

    # 当前用户录入后未复核的终端记录
    terms_input = []
    for term in req.session.notify.terms_input
        await client.query \
            "select t.*, m.name as mer_name, m.merno as mer_no
                from pay_terms_ex as t
                left join pay_merch as m on m.uuid = t.merid
                where t.uuid = $1", [
                term.uuid
            ], defer err, result
        if err then done(client); return next(err)
        terms_input.push result.rows...
    res.locals.notify.terms_input = terms_input

    # 当前用户复核后未生效的终端记录
    terms_wait_effect = []
    for term in req.session.notify.terms_wait_effect
        await client.query \
            "select t.*, m.name as mer_name, m.merno as mer_no
                from pay_terms_ex as t
                left join pay_merch as m on m.uuid = t.merid
                where uuid = $1", [
                term.uuid
            ], defer err, result
        if err then done(client); return next(err)
        terms_wait_effect.push result.rows...
    res.locals.notify.terms_wait_effect = terms_wait_effect

    done()
    res.render 'notify/index'


router.post '/notification/delete', (req, res, next) ->
    res.type('json')
    for k in ['serial']
        return next(new Error("请求缺少参数[#{k}]")) if not req.body[k]

    if not req.session.userinfo.remove_notify
        return next new Error('权限拒绝')

    await pgsql.connect settings.pgsql_url, defer err, client, done
    if err then done(client); return next(err)

    await client.query \
        "update web_notify set status = 2 where serial = $1", [
            req.body.serial,
        ], defer err, result
    if err then done(client); return next(err)
    done()
    res.json succ: true, redirect: '/notify'


router.post '/notification/clear', (req, res, next) ->
    res.type('json')

    if not req.session.userinfo.remove_notify
        return next new Error('权限拒绝')

    await pgsql.connect settings.pgsql_url, defer err, client, done
    if err then done(client); return next(err)

    await client.query \
        "update web_notify set status = 2", defer err, result
    if err then done(client); return next(err)
    done()
    res.json succ: true, redirect: '/notify'


router.post '/refresh', (req, res, next) ->
    res.type('json')
    req.app.set 'notify_modified_time', moment().format('x')
    res.json succ: true
